﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Finance.Entites;
using Finance.UI.ViewModels;

namespace Finance.UI.UserControls
{
    public partial class CycleSummaryPivotItemUserControl : UserControl
    {
        public string CycleContent { get; set; }
        public EventHandler Click { get; set; }
        public Period PeriodContent { get; set; }

        private Func<Charge, Period, bool> periodPredicate =
            (charge, period) =>
            {
                switch (period)
                {
                    case Period.Day:
                        {
                            return charge.Date.ToShortDateString() == DateTime.Today.ToShortDateString();
                        }
                    case Period.Week:
                        {
                            var startdate = DateTime.Today.AddDays((-((int)DateTime.Today.DayOfWeek + 6) % 7));
                            var enddate = startdate.AddDays(6);
                            return charge.Date > startdate && charge.Date < enddate.AddDays(1);
                        }
                    case Period.Month:
                        {
                            return charge.Date.Month == DateTime.Today.Month
                                && charge.Date.Year == DateTime.Today.Year;
                        }
                    default: return charge.Date.ToShortDateString() == DateTime.Today.ToShortDateString();
                }

            };

        private double IncomeAmount
        {
            get
            {
                return App.ChargeDataStore.Items.Where(
                    charge => charge is IncomeCharge && periodPredicate(charge, PeriodContent))
                    .Sum(charge => charge.Amount);
            }
        }

        private double PayoutAmount
        {
            get
            {
                return App.ChargeDataStore.Items.Where(
                    charge => charge is PayoutCharge && periodPredicate(charge, PeriodContent))
                    .Sum(charge => charge.Amount);
            }
        }

        public CycleSummaryPivotItemUserControl()
        {
            InitializeComponent();
            this.Loaded += (o, e) => { InitializeDataContext(); };
        }

        private void InitializeDataContext()
        {
            var R = new Random();
            CycleSummaryViewModel dataContext = new CycleSummaryViewModel(
                this.CycleContent ?? "Unknown");
            //dataContext.IconUri = new Uri("/ApplicationIcon.png", UriKind.RelativeOrAbsolute);
            dataContext.Income = IncomeAmount;
            dataContext.Payout = PayoutAmount;

            ControlRoot.DataContext = dataContext;
        }

        private void LinkToPeriodicDetails_Click(object sender, RoutedEventArgs e)
        {
            Click(sender, e);
        }
    }
}
